﻿using Dapper;
using System.Data.SqlClient;
using yunbo.Model.RMM;
using yunbo.Model.SM;

namespace yunbo.Repositorys.SMRepositorys
{
    public class WMS_LLCKRepository
    {
        public string data = "Data Source=121.37.93.71;Initial Catalog=FanXingMES;User ID=sa;Password=geili@2025;";

        public FYCXWMS_LLCK GetFYCXWMS_LLCK(int PageNumber, int PageSize)
        {
            using (SqlConnection connection = new SqlConnection(data))
            {
                string sql = $@"select 
	                            ckxx.*,
	                            us.[user_name] as user1_name
                                  from [dbo].[wms_llck] ckxx
                            left join
                             [dbo].[user] us on ckxx.create_by=us.user_id
                                 ORDER BY  [llck_id] 
                               OFFSET ({PageNumber}-1)*{PageSize} ROWS
                               FETCH NEXT {PageSize} ROWS ONLY;";
                List<WMS_LLCK> lLCKs = connection.Query<WMS_LLCK>(sql).ToList();
                string sql1 = "SELECT COUNT(*) FROM [dbo].[wms_llck]";
                int zon = connection.Query<int>(sql1).FirstOrDefault();
               FYCXWMS_LLCK fYCXWMS_LLCK = new FYCXWMS_LLCK()
               {
                   wMS_LLCKs = lLCKs,
                   zon = zon,
               };
                return fYCXWMS_LLCK;
            }
        }
        public int Addllck(WMS_LLCK wMS_LLCK)
        {
            using (SqlConnection connection = new SqlConnection(data))
            {
                string sql = $@"insert into [dbo].[wms_llck](llck_code, llck_lx, llck_gdh, llck_jhllsj, llck_bz, create_by, create_time, llck_zt)
                                values('{wMS_LLCK.llck_code}','{wMS_LLCK.llck_lx}','{wMS_LLCK.llck_gdh}','{wMS_LLCK.llck_jhllsj}','{wMS_LLCK.llck_bz}','{wMS_LLCK.create_by}','{wMS_LLCK.create_time}','{wMS_LLCK.llck_zt}')";
                int ll=connection.Execute(sql);
                return ll;
            }
        }
        public int UpZt(string code)
        {
            using (SqlConnection connection = new SqlConnection(data))
            {
                string sql = $@"update [dbo].[wms_llck] set [llck_zt]='2' where [llck_code]='{code}'";
                int zt = connection.Execute(sql);
                return zt;
            }
        }

        public WMS_LLCK GetWMS_LLCK(string code)
        {
            using (SqlConnection connection = new SqlConnection(data))
            {
                string sql = $@"select * from [dbo].[wms_llck] where llck_gdh='{code}'";
                WMS_LLCK lLCKs = connection.Query<WMS_LLCK>(sql).FirstOrDefault() ;
                return lLCKs;
            }
        }
    }
}
